package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.scache.httpreq;

public class Poll2_InterCall extends QQInterfaceCall {

	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ)
			throws Exception {
/**
r:{"clientid":"62996693","psessionid":"8368046764001d636f6e6e7365727665725f77656271714031302e3133342e362e31333800004a4a0000138a036e0400d8e8885e6d0000000a40446633324c545745716d000000284414cd21b0fc38b150e46cb7cc638b97a866071fd8460fd44b85665fd881cd38daabc17de1560ab1"
	,"key":0,"ids":[]}
clientid:62996693
psessionid:8368046764001d636f6e6e7365727665725f77656271714031302e3133342e362e31333800004a4a0000138a036e0400d8e8885e6d0000000a40446633324c545745716d000000284414cd21b0fc38b150e46cb7cc638b97a866071fd8460fd44b85665fd881cd38daabc17de1560ab1
 */
		StringBuilder bld = new StringBuilder();
		bld.append("r={\"clientid\":\"").append(loginQQ.getClientId()).append("\",");
			bld.append("\"psessionid\":\"").append(loginQQ.getPsessionid()).append("\",");
			bld.append("\"key\":0,\"ids\":[]}");
		bld.append("&clientid=").append(loginQQ.getClientId());
		bld.append("&psessionid=").append(loginQQ.getPsessionid());
		return bld.toString();
	}

	@Override
	public int getLogLevel() {
		return 1001;
	}

	@Override
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ)
			throws JSONException {
		return "http://d.web2.qq.com/channel/poll2";
	}

	@Override
	public int getReq_method() {
		return httpreq.REQUEST_POST;
	}

	@Override
	public boolean hasPreCall(QQLogedin loginQQ, JSONObject reqDataObj) {
		return false;
	}

	@Override
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
		ArrayList<String> s = buildReqHeaders(reqBodyLen, loginQQ,
				"http://d.web2.qq.com/proxy.html?v=20110331002&callback=2", 
				"d.web2.qq.com", "http://d.web2.qq.com");
		s.add("Content-Type: application/x-www-form-urlencoded");
		return s;
	}

	@Override
	public JSONObject preCall(QQLogedin loginQQ, JSONObject reqDataObj)
			throws Exception {
		return null;
	}

	
	private static Poll2_InterCall instance;
	public static synchronized Poll2_InterCall getInstance() {
		if(instance == null)
			instance = new Poll2_InterCall();
		return instance;
	}
}
